package com.dylankenny.flikrreader.model.parse;

import com.dylankenny.flikrreader.model.data.Feed;
import com.dylankenny.flikrreader.model.data.FeedItem;
import com.dylankenny.flikrreader.model.data.Media;
import com.google.gson.Gson;

/**
 * Utility class to do JSON conversions between data objects and JSON strings
 * 
 * @author cathbar
 */
public class JsonFeedCodec {

	public static String serializeFeedItem(FeedItem item) {
		Gson gson = new Gson();
		return gson.toJson(item);
	}

	public static FeedItem deserializeFeedItem(String json) {
		Gson gson = new Gson();
		FeedItem feedItem = gson.fromJson(json, FeedItem.class);
		if (feedItem.getFilePath() == null || feedItem.getFilePath() == "") {

			feedItem.setFilePath(NetworkManager.PATH + feedItem.getMedia().getM().substring(feedItem.getMedia().getM().lastIndexOf("/") + 1));
		}
		return feedItem;
	}

	public static String serializeFeed(Feed feed) {
		Gson gson = new Gson();
		return gson.toJson(feed);
	}

	public static Feed deserializeFeed(String json) {
		Gson gson = new Gson();
		return gson.fromJson(json, Feed.class);
	}

	public static String serializeTuple(Media tuple) {
		Gson gson = new Gson();
		return gson.toJson(tuple);
	}

	public static Media deserializeTuple(String json) {
		Gson gson = new Gson();
		return gson.fromJson(json, Media.class);
	}
}
